package io.github.talelin.latticy.service.impl;

import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.order.OrderDTO;
import io.github.talelin.latticy.mapper.SpuMapper;
import io.github.talelin.latticy.mapper.WxUserMapper;
import io.github.talelin.latticy.model.OrderDO;
import io.github.talelin.latticy.mapper.OrderMapper;
import io.github.talelin.latticy.model.SpuDO;
import io.github.talelin.latticy.model.WxUserDO;
import io.github.talelin.latticy.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator@lms
 * @since 2021-04-28
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderDO> implements OrderService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private WxUserMapper wxUserMapper;


    @Override
    public void placeAnOrder(OrderDTO dto) {
        SpuDO spu = spuMapper.selectById(dto.getSpuId());
        if (spu == null) {
            throw new NotFoundException(80400);
        }
        WxUserDO wxUser = wxUserMapper.selectById(dto.getUserId());
        if (wxUser == null) {
            throw new NotFoundException(20000);
        }
        OrderDO order = new OrderDO();
        order.setTotalPrice(spu.getPrice())
                .setSpuId(spu.getId())
                .setAddress(dto.getAddress())
                .setIsPay(0)
                .setUserId(wxUser.getId());
        getBaseMapper().insert(order);
    }
}
